package com.cqcetbbs.smartlab.cqcetbbs.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cqcetbbs.smartlab.cqcetbbs.R;
import com.cqcetbbs.smartlab.cqcetbbs.javabean.new_art;

import java.util.ArrayList;

import static com.cqcetbbs.smartlab.cqcetbbs.R.id.img;
import static com.cqcetbbs.smartlab.cqcetbbs.R.id.tv_title;

/**
 * Created by user on 2017/10/6.
 */

public class FragmentDetailAdapter extends BaseAdapter {

    private static  final   String URL = "http://api.cqcetlab.com/Home/article/artlist";
    private Context context;
    private ArrayList<new_art.Art> artList;

    public FragmentDetailAdapter(Context context, ArrayList<new_art.Art> artList) {

        super();
        this.context = context;
        this.artList = artList;
        for (int i = 0; i < artList.size(); i++) {
            System.out.println("name [" + i + "] = " + artList.get(i).getTitle());
        }
    }

    @Override
    public int getCount() {
        return artList.size();
    }

    @Override
    public new_art.Art getItem(int position) {
        return artList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.fragment_detail_item, null);
            viewHolder.tv_title = convertView.findViewById(tv_title);
            viewHolder.tv_time = convertView.findViewById(R.id.tv_time);
            viewHolder.tv_read = convertView.findViewById(R.id.tv_read);
            viewHolder.img = convertView.findViewById(img);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

            Glide.with(context).load(artList.get(position).getPic()).centerCrop().into(viewHolder.img);
            viewHolder.tv_title.setText(artList.get(position).getTitle());
            viewHolder.tv_time.setText(artList.get(position).getUp_time());
            viewHolder.tv_read.setText("阅读量:"+artList.get(position).getClick());

        return convertView;
    }


    public static class ViewHolder {
        TextView tv_title;
        TextView tv_read;
        TextView tv_time;
        ImageView img;
    }

}
